package com.example.news123.data;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.news123.R;

import java.util.List;

public class ContAdapter extends RecyclerView.Adapter<ContAdapter.ViewHolder> {
    private List<News> list;
    private Context context;
    public ContAdapter(Context context,List<News> list){
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news = list.get(position);
        holder.bigTitle.setText(news.getBigTitle());
        holder.take.setText(news.getTake());
        holder.time.setText(news.getTime());
        Picasso.get().load(news.getImageUrl()).into(holder.imageNews);
        String mUrl = (news.getUrl());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Tag",mUrl);
                Toast.makeText(context, "我被点击了", Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(context,ContActivity.class);
//                intent.putExtra("url",mUrl);
//                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView bigTitle,take,time;
        ImageView imageNews;

        public ViewHolder(@NonNull View itemView){
            super(itemView);

            bigTitle = itemView.findViewById(R.id.bigTitle);
            take = itemView.findViewById(R.id.take);
            time = itemView.findViewById(R.id.time);
            imageNews = itemView.findViewById(R.id.imageNews);

        }
    }
}


